package com.lrs.java.jdbc.jdbc_teacher;
/*	范例名称：数据库元数据方法应用举例
 * 	源文件名称：DBMetaDataMethods.java 
 *	要  点：
 *            1.  getTables方法的使用
 *            2.  getPrimaryKeys方法的使用
 */

import java.sql.*;

class DBMetaDataMethods {

	public static void main(String args[]) {

		String serverName = "127.0.0.1";
		Connection con;
		Statement stmt;

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");

		} catch (java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			//con = DriverManager.getConnection(url, "", "");
			String url = "jdbc:oracle:thin:@" + serverName + ":1521:SXT";
			con = DriverManager.getConnection(url, "scott", "tiger");
			stmt = con.createStatement();
			DatabaseMetaData md = con.getMetaData();
			ResultSet rs = md.getTables(null, "SCOTT", null,
					new String[] { "TABLE" });

			while (rs.next()) {

				String s = rs.getString(3);
				System.out.println(s + "  ");

			}
			System.out.println("-------------------");
			ResultSet rs1 = md.getPrimaryKeys(null, null, "EMP");
			while (rs1.next()) {
				for (int i = 1; i < 7; i++) {
					String s3 = rs1.getString(i);
					System.out.println(s3);
				}

			}
			stmt.close();
			con.close();

		} catch (SQLException ex) {
			//System.err.println("SQLException: " + ex.getMessage());
			ex.printStackTrace();
		}
	}
}
